from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import numpy as np

"""
CHAR BANK FORMAT:
    A:
    char mat
    B:
    char mat
    C:
    char mat
    ...
"""

init_wh = 100  # width and height
init_size = (init_wh, init_wh)
img_wh = 20
img_size = (img_wh, img_wh)


font = ImageFont.truetype(r'C:\Windows\Fonts\Times.ttf', 100)


def gen_char_pic(ch):
    im =Image.new('L', init_size, 0)
    draw = ImageDraw.Draw(im)
    draw.text((0, -15), ch , fill=255, font=font)

    im = im.resize(img_size)
    return im


def gen_char_mat(im):
    arr = np.asarray(im)
    arr = np.asarray(arr > 0, dtype=np.uint8)

    mat = ''
    for row in arr:
        for val in row:
            mat += str(val)
        mat += '\n'

    return mat


def gen_char_bank():
    with open('CharBank.txt', 'w') as f:
        for i in range(33, 128):
            # visible character starts from 33 and 128 is back space
            im = gen_char_pic(chr(i))
            mat = gen_char_mat(im)

            f.write('%s\n' % chr(i))
            f.write(mat)

    f.close()


num_rank = dict()

gen_char_bank()
print('done')
